//
//  HAHelper.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAHelper.h"
#import <AVFoundation/AVFoundation.h>
#import "SDWebImageDownloader.h"
#import "SDImageCache.h"

@implementation HAHelper


+(void)keepUserInfo: (HAUserInfoModel *)model {
    
    HAUserDefaults.address = model.address;
    HAUserDefaults.agent_id = model.agent_id;
    HAUserDefaults.avatar = model.avatar;
    HAUserDefaults.birthday = model.birthday;
    HAUserDefaults.createtime = model.createtime;
    HAUserDefaults.deposit = model.deposit;
    HAUserDefaults.email = model.email;
    HAUserDefaults.expires_in = model.expires_in;
    HAUserDefaults.expiretime = model.expiretime;
    HAUserDefaults.gender = model.gender;
    HAUserDefaults.userID = model.userID;
    HAUserDefaults.identity = model.identity;
    HAUserDefaults.level = model.level;
    HAUserDefaults.logintime = model.logintime;
    HAUserDefaults.mobile = model.mobile;
    HAUserDefaults.name = model.name;
    HAUserDefaults.oem_id = model.oem_id;
    HAUserDefaults.parent_id = model.parent_id;
    HAUserDefaults.region = model.region;
    HAUserDefaults.status = model.status;
    HAUserDefaults.token = model.token;
    HAUserDefaults.type = model.type;
    HAUserDefaults.username = model.username;
    HAUserDefaults.isLock = model.isLock;
}

+(void)outUserInfo {
    HAUserDefaults.address = nil;
    HAUserDefaults.agent_id = nil;
    HAUserDefaults.avatar = nil;
    HAUserDefaults.birthday = nil;
    HAUserDefaults.createtime = nil;
    HAUserDefaults.deposit = nil;
    HAUserDefaults.email = nil;
    HAUserDefaults.expires_in = nil;
    HAUserDefaults.expiretime = nil;
    HAUserDefaults.gender = nil;
    HAUserDefaults.userID = nil;
    HAUserDefaults.identity = nil;
    HAUserDefaults.level = 0;
    HAUserDefaults.logintime = nil;
//    HAUserDefaults.mobile = nil;
    HAUserDefaults.name = nil;
    HAUserDefaults.oem_id = nil;
    HAUserDefaults.parent_id = nil;
    HAUserDefaults.region = nil;
    HAUserDefaults.status = 0;
    HAUserDefaults.token = nil;
    HAUserDefaults.type = 0;
    HAUserDefaults.username = nil;
    HAUserDefaults.isLock = 0;

    
}

//跳转到设置
+(void)GetPhoneSet {
    NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
    if ( [[UIApplication sharedApplication] canOpenURL: url] ) {
        NSURL*url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

//是否允许访问相机
+(BOOL)isCamera {
    
    AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        return NO;
        //无权限
    }else {
        return YES;
    }
}

+(void)toPhone:(NSString *)mobile {
    NSMutableString * str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",mobile];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVc {
    NSMutableArray *Marray = [NSMutableArray arrayWithArray: [UIApplication sharedApplication].windows];
    
    [Marray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIWindow *win = obj;
        if (win.hidden == YES) {
            [Marray removeObject:win];
        }
    }];
    
    UIWindow *window = [Marray firstObject];
    
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }
    
    id nextResponder = [tempView nextResponder];
    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];
        
        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}

//获取产品渠道
+(void)productChannelComplete:(void(^)(NSArray *array))complete {
    
    NSDictionary *dict = @{
        @"isAll":@"1",
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"canal/item"];
    request1.requestMethod = SYRequestMethodPOST;
    request1.parms = dict;

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                if (complete) {
                    complete(nil);
                }

                return;
            }

            if (dataModel.code == 1) {
                NSArray *array = [HACanalItemMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                
                [[HASingle single].canalItemArray removeAllObjects];
                [[HASingle single].canalItemArray addObjectsFromArray:array];
                
                if (complete) {
                    complete(array);
                }
                
            }else{
                [MBProgressHUD showError:dataModel.msg];
                
                if (complete) {
                    complete(nil);
                }
            }
        }
    }];
}

+(void)hl_sysdownloadImagesWith:(NSArray *)array complete: (void(^)(UIImage *imaege, NSInteger index))complete {
    
    SDImageCache *cache = [SDImageCache sharedImageCache];
    SDWebImageDownloader *loader = [SDWebImageDownloader sharedDownloader];
    
    for (NSInteger i = 0; i < array.count; i ++) {
        id img = array[i];
        if ([img isKindOfClass:[NSString class]]) {
            UIImage *sd_image = [cache imageFromCacheForKey:img];
            if (sd_image) {
                if (complete) {
                    complete(sd_image,i);
                }
            }else{
                [loader downloadImageWithURL:[NSURL URLWithString:img] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                    if (image) {
                        if (complete) {
                            complete(image,i);
                        }
                        [cache storeImage:image forKey:img completion:nil];
                    }
                }];
            }
        }else if ([img isKindOfClass:[UIImage class]]) {
            if (complete) {
                complete(img,i);
            }
        }
    }
}


@end
